package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.vo.WipMarkingElSummaryVO;
import com.xdcplus.web.mapper.WipMarkingIvSummaryMapper;
import com.xdcplus.web.common.pojo.entity.WipMarkingIvSummary;
import com.xdcplus.web.common.pojo.dto.WipMarkingIvSummaryDTO;
import com.xdcplus.web.common.pojo.dto.WipMarkingIvSummaryFilterDTO;
import com.xdcplus.web.common.pojo.vo.WipMarkingIvSummaryVO;
import com.xdcplus.web.common.pojo.query.WipMarkingIvSummaryQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingIvSummaryService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * (WipMarkingIvSummary)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-11-28 18:34:28
 */
@Slf4j
@Service("wipMarkingIvSummaryService")
public class WipMarkingIvSummaryServiceImpl extends BaseServiceImpl<WipMarkingIvSummaryMapper, WipMarkingIvSummary, WipMarkingIvSummary, WipMarkingIvSummaryVO> implements WipMarkingIvSummaryService {

    @Autowired
    protected WipMarkingIvSummaryMapper wipMarkingIvSummaryMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingIvSummary(WipMarkingIvSummaryDTO wipMarkingIvSummaryDTO) {

        WipMarkingIvSummary wipMarkingIvSummary = new WipMarkingIvSummary();
        BeanUtil.copyProperties(wipMarkingIvSummaryDTO, wipMarkingIvSummary);
        wipMarkingIvSummary.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(wipMarkingIvSummary);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingIvSummary(WipMarkingIvSummaryDTO wipMarkingIvSummaryDTO) {

        WipMarkingIvSummary wipMarkingIvSummary = this.getById(wipMarkingIvSummaryDTO.getId());
        if (ObjectUtil.isNull(wipMarkingIvSummary)) {
            log.error("updateWipMarkingIvSummary() The WipMarkingIvSummary does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingIvSummaryDTO, wipMarkingIvSummary);
        wipMarkingIvSummary.setUpdatedUser(wipMarkingIvSummaryDTO.getUpdatedUser());
        wipMarkingIvSummary.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingIvSummary);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingIvSummary> wipMarkingIvSummaryList) {

        wipMarkingIvSummaryList.forEach(wipMarkingIvSummary -> {
            WipMarkingIvSummary wipMarkingIvSummaryParam = new WipMarkingIvSummary();
            wipMarkingIvSummaryParam.setId(wipMarkingIvSummary.getId());
            if (ObjectUtil.isNotNull(wipMarkingIvSummary.getId())) {
                wipMarkingIvSummary.setId(wipMarkingIvSummary.getId());
                wipMarkingIvSummary.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingIvSummary> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingIvSummary::getId, wipMarkingIvSummary.getId());
                update(wipMarkingIvSummary, lambdaUpdate);
            } else {
                wipMarkingIvSummary.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingIvSummary);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingIvSummaryDTO> wipMarkingIvSummaryDTOList) {

        List<WipMarkingIvSummary> wipMarkingIvSummaryList = BeanUtils.copyProperties(wipMarkingIvSummaryDTOList, WipMarkingIvSummary.class);
        return saveOrUpdateBatch(wipMarkingIvSummaryList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingIvSummaryLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingIvSummary wipMarkingIvSummary = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingIvSummary)) {
            log.error("deleteWipMarkingIvSummary() The WipMarkingIvSummary does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingIvSummaryByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingIvSummary wipMarkingIvSummary = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingIvSummary)) {
				log.error("deleteWipMarkingIvSummary() The WipMarkingIvSummary does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingIvSummary> queryWipMarkingIvSummaryList(WipMarkingIvSummaryFilterDTO wipMarkingIvSummaryFilterDTO) {
        WipMarkingIvSummaryQuery wipMarkingIvSummaryQuery = BeanUtil.copyProperties(wipMarkingIvSummaryFilterDTO, WipMarkingIvSummaryQuery.class);

        return wipMarkingIvSummaryMapper.queryWipMarkingIvSummary(wipMarkingIvSummaryQuery);
    }

    @Override
    public List<WipMarkingIvSummaryVO> queryWipMarkingIvSummaryVOList(WipMarkingIvSummaryFilterDTO wipMarkingIvSummaryFilterDTO) {
        return this.objectConversion(queryWipMarkingIvSummaryList(wipMarkingIvSummaryFilterDTO));
    }

    @Override
    public PageVO<WipMarkingIvSummaryVO> queryWipMarkingIvSummary(WipMarkingIvSummaryFilterDTO wipMarkingIvSummaryFilterDTO) {
        PageVO<WipMarkingIvSummaryVO> pageVO = new PageVO<>();

        if (wipMarkingIvSummaryFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingIvSummaryFilterDTO.getCurrentPage(), wipMarkingIvSummaryFilterDTO.getPageSize(),
                    wipMarkingIvSummaryFilterDTO.getOrderType(), wipMarkingIvSummaryFilterDTO.getOrderField());
        }

        List<WipMarkingIvSummary> wipMarkingIvSummaryList = queryWipMarkingIvSummaryList(wipMarkingIvSummaryFilterDTO);

        PageInfo<WipMarkingIvSummary> pageInfo = new PageInfo<>(wipMarkingIvSummaryList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingIvSummaryList));

        return pageVO;
    }

    @Override
    public WipMarkingIvSummaryVO queryWipMarkingIvSummaryById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<WipMarkingIvSummaryVO> queryByMarkingId(Long markingId) {
        return this.objectConversion(wipMarkingIvSummaryMapper.queryByMarkingId(markingId));
    }
}
